#!/usr/bin/env bash
# notify when mpd music changed

COVER="/tmp/cover.png"
# mpd music directory
MUSIC_DIR="$HOME/Music"
# if cover not found in metadata use this instead
BACKUP_COVER="~/dotfiles/eww/images/music.png"

get_cover() {
	ffmpeg -i "${MUSIC_DIR}"/"$(mpc current -f %file%)" "${COVER}" -y &> /dev/null
	STATUS=$?

	# Check if the file has a embbeded album art
	if [ "$STATUS" -eq 0 ];then
		echo "$COVER"
	else
		echo "$BACKUP_COVER"
	fi
}

while "true"; do
    status="`mpc status`"
    if [[ ${status} == *"[playing]"* ]]; then
        dunstify -a Music --replace=27072 -t 2000 -i $(get_cover) "$(mpc --format '%title%-%artist%' current)"
    fi
    mpc current --wait > /dev/null
done
